Imports Security.Entities
Imports DataAccess.Base

Public Class UserViewDAO
    Inherits ViewDAO(Of UserView)

    Public Sub New()
        Me.TableName = "Security_UsersView"
        Me.IDColumn = "UserId"
    End Sub

    Public Function ListWithFilters(ByVal username As String, ByVal email As String, ByVal tenantId As Guid, ByVal applicationId As Guid, ByVal activated As Boolean) As IList(Of UserView)
        Dim query As String = String.Format("SELECT * FROM [{0}]", Me.TableName)
        Dim where As String = String.Empty

        If Not String.IsNullOrEmpty(username) Then
            where &= String.Format("UserName Like '{0}%' AND ", username.Replace("'", "''"))
        End If

        If Not String.IsNullOrEmpty(email) Then
            where &= String.Format("Email Like '{0}%' AND ", email.Replace("'", "''"))
        End If

        If tenantId <> Guid.Empty Then
            where &= String.Format("TenantId = '{0}' AND ", tenantId)
        End If

        If applicationId <> Guid.Empty Then
            where &= String.Format("ApplicationId = '{0}' AND ", applicationId)
        End If

        If activated Then
            where &= "Activated = 1 AND "
        Else
            where &= "Activated = 0 AND "
        End If

        If Not String.IsNullOrEmpty(where) Then
            query &= " WHERE " & where.Substring(0, where.Length - 5)
        End If

        Return ListByQuery(query)
    End Function
End Class
